`
https://leetcode.cn/problems/count-number-of-nice-subarrays/
`

/**
 * @param {number[]} nums
 * @param {number} k
 * @return {number}
 */
var numberOfSubarrays = function (nums, k) {
  return solve(nums, k) - solve(nums, k + 1)
};

// 计算某个连续子数组中有 >= k 个奇数数字的子数组个数
function solve(nums, k) {
  const n = nums.length
  let count = 0
  let res = 0
  let left = 0, right = 0

  while (right < n) {
    const n1 = nums[right++]
    if (n1 % 2 === 1) count++

    // 关注 left - 1 的合法性
    while (count === k) {
      const n2 = nums[left++]
      if (n2 % 2 === 1) count--
    }

    // 有 left 个
    res += left
  }

  return res
}